package com.hhx.baselibrary.app;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;

import com.hhx.baselibrary.BuildConfig;
import com.hhx.baselibrary.logger.LoggerUtil;

/**
 * Created by Administrator on 2017/7/5.
 */

public class BaseApplication extends Application {

    private int count = 0;
    private static BaseApplication baseApplication;
    //

    @Override
    public void onCreate() {
        super.onCreate();
        baseApplication = this;
        LoggerUtil.init(BuildConfig.DEBUG);

        registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(Activity activity) {
                count++;
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
                if (count > 0) {
                    count--;
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }

    public static BaseApplication getAppContext() {
        return baseApplication;
    }

    public static Resources getReouces() {
        return baseApplication.getResources();
    }

    /**
     * 获取应用版本号，默认返回1
     *
     * @return
     */
    public static int getAppVersionCode() {
        Context context = getAppContext();
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);

            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 1;
    }

    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    /**
     * 判断app是否在后台
     *
     * @return
     */
    public boolean isInBackground() {
        if (count <= 0) {
            return true;
        } else {
            return false;
        }
    }
}
